/**
 * 
 */
package br.com.framework.negocio.impl;

import java.util.List;
import java.util.Map;

import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.PersistenceException;

import br.com.framework.negocio.api.BaseNegocio;
import br.com.framework.negocio.exception.NegocioException;
import br.com.framework.persistencia.api.BaseDao;
import br.com.framework.persistencia.model.BaseEntity;
import br.com.framework.persistencia.sort.Order;

/**
 * @author Cleber
 *
 */
//Indica que o gerenciamento das trasações será realizado pelo conteiner
@TransactionManagement(TransactionManagementType.CONTAINER)
//Indica que os métodos dessa classe utilizam uma transação nova
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public abstract class BaseNegocioImpl<E extends BaseEntity, DAO extends BaseDao<E>> implements BaseNegocio<E, DAO> {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private DAO dao;

	/**
	 * 
	 */
	public BaseNegocioImpl() {
	}
	
	/**
	 * @return the dao
	 */
	protected DAO getDao() {
		return dao;
	}

	/**
	 * @param dao the dao to set
	 */
	protected void setDao(DAO dao) {
		this.dao = dao;
	}

	/* (non-Javadoc)
	 * @see br.com.framework.negocio.api.BaseNegocio#buscarPorID(java.lang.Long)
	 */
	@Override
	public E findByID(Long id) throws PersistenceException {
		return getDao().getByID(id);
	}

	/* (non-Javadoc)
	 * @see br.com.framework.negocio.api.BaseNegocio#buscarTodos()
	 */
	@Override
	public List<E> findAll() throws PersistenceException {
		return getDao().findAll();
	}

	/* (non-Javadoc)
	 * @see br.com.framework.negocio.api.BaseNegocio#buscarTodos(boolean)
	 */
	@Override
	public List<E> findAll(boolean onlyActive) throws PersistenceException {
		return getDao().findAll(onlyActive);
	}

	/* (non-Javadoc)
	 * @see br.com.framework.negocio.api.BaseNegocio#findAll(int, int)
	 */
	@Override
	public List<E> findAll(int firstResult, int maxResults) throws PersistenceException {
	    return findAll(firstResult, maxResults, true);
	}

	/* (non-Javadoc)
	 * @see br.com.framework.negocio.api.BaseNegocio#findAll(int, int, boolean)
	 */
	@Override
	public List<E> findAll(int firstResult, int maxResults, boolean onlyActive) throws PersistenceException {
	    return findAll(firstResult, maxResults, null, null, onlyActive);
	}
	
	/* (non-Javadoc)
	 * @see br.com.framework.negocio.api.BaseNegocio#findAll(int, int, java.lang.String, br.com.framework.persistencia.sort.Order, boolean)
	 */
	@Override
	public List<E> findAll(int firstResult, int maxResults,
			String sortField, Order sortOrder, boolean onlyActive)
			throws PersistenceException {
		return findAll(firstResult, maxResults, sortField, sortOrder, null, onlyActive);
	}
	
	/* (non-Javadoc)
	 * @see br.com.framework.negocio.api.BaseNegocio#findAll(java.lang.String, br.com.framework.persistencia.sort.Order, boolean)
	 */
	@Override
	public List<E> findAll(String sortField, Order sortOrder,
			boolean onlyActive) throws PersistenceException {
		return findAll(-1, -1, sortField, sortOrder, null, onlyActive);
	}
	
	/* (non-Javadoc)
	 * @see br.com.framework.negocio.api.BaseNegocio#findAll(int, int, java.lang.String, br.com.framework.persistencia.sort.Order, java.util.Map, boolean)
	 */
	@Override
	public List<E> findAll(int firstResult, int maxResults,
			String sortField, Order sortOrder, Map<String, String> filters,
			boolean onlyActive) throws PersistenceException {
		return getDao().findAll(firstResult, maxResults, sortField, sortOrder, filters, onlyActive);
	}

	/* (non-Javadoc)
	 * @see br.com.framework.negocio.api.BaseNegocio#inserir(br.com.framework.persistencia.model.BaseEntity)
	 */
	@Override
	public void insert(E entidade) throws PersistenceException,
			NegocioException {
		getDao().insert(entidade);
	}

	/* (non-Javadoc)
	 * @see br.com.framework.negocio.api.BaseNegocio#editar(br.com.framework.persistencia.model.BaseEntity)
	 */
	@Override
	public void update(E entidade) throws PersistenceException,
			NegocioException {
		getDao().update(entidade);
	}

	/* (non-Javadoc)
	 * @see br.com.framework.negocio.api.BaseNegocio#atualizar(br.com.framework.persistencia.model.BaseEntity)
	 */
	@Override
	public void refresh(E entidade) throws PersistenceException {
		getDao().refresh(entidade);
	}

	/* (non-Javadoc)
	 * @see br.com.framework.negocio.api.BaseNegocio#remover(br.com.framework.persistencia.model.BaseEntity)
	 */
	@Override
	public void remove(E entidade) throws PersistenceException,
			NegocioException {
		remove(entidade, false);
	}

	/* (non-Javadoc)
	 * @see br.com.framework.negocio.api.BaseNegocio#remover(br.com.framework.persistencia.model.BaseEntity, boolean)
	 */
	@Override
	public void remove(E entidade, boolean removerDefinitivamente)
			throws PersistenceException, NegocioException {
		getDao().remove(entidade, removerDefinitivamente);
	}
	
	/* (non-Javadoc)
	 * @see br.com.framework.negocio.api.BaseNegocio#remover(java.io.Serializable)
	 */
	@Override
	public void remove(Long id) throws PersistenceException, NegocioException {
		remove(id, false);
	}

	/* (non-Javadoc)
	 * @see br.com.framework.negocio.api.BaseNegocio#remover(java.io.Serializable, boolean)
	 */
	@Override
	public void remove(Long id, boolean removerDefinitivamente)
			throws PersistenceException, NegocioException {
		getDao().remove(id, removerDefinitivamente);
	}

	/* (non-Javadoc)
	 * @see br.com.framework.negocio.api.BaseNegocio#getTotalRegistros()
	 */
	@Override
	public Long getRecordCount() throws PersistenceException {
		return getDao().getRecordCount();
	}

	/* (non-Javadoc)
	 * @see br.com.framework.negocio.api.BaseNegocio#getTotalRegistros(boolean)
	 */
	@Override
	public Long getRecordCount(boolean onlyActive)
			throws PersistenceException {
		return getDao().getRecordCount(onlyActive);
	}

	@Override
	public Long getRecordCount(Map<String, String> filters,
			boolean onlyActive) throws PersistenceException {
		return getDao().getRecordCount(filters, onlyActive);
	}
	
	/* (non-Javadoc)
	 * @see br.com.framework.negocio.api.BaseNegocio#findByExample(br.com.framework.persistencia.model.BaseEntity, int, int, java.lang.String, br.com.framework.persistencia.sort.Order, boolean, boolean)
	 */
	public List<E> findByExample(E entidade, int firstResult, int maxResults, String sortField, Order sortOrder, boolean isStringCaseSensitive, boolean isLikeEnabled) throws PersistenceException {
		return getDao().findByExample(entidade, firstResult, maxResults, sortField, sortOrder, isStringCaseSensitive, isLikeEnabled);
	}
	
	/* (non-Javadoc)
	 * @see br.com.framework.negocio.api.BaseNegocio#findByExample(br.com.framework.persistencia.model.BaseEntity, boolean, boolean)
	 */
	public List<E> findByExample(E entidade, boolean isStringCaseSensitive, boolean isLikeEnabled) throws PersistenceException{
		return findByExample(entidade, -1, -1, null, null, isStringCaseSensitive, isLikeEnabled);
	}
	
	/* (non-Javadoc)
	 * @see br.com.framework.negocio.api.BaseNegocio#findByExample(br.com.framework.persistencia.model.BaseEntity, java.lang.String, br.com.framework.persistencia.sort.Order)
	 */
	public List<E> findByExample(E entidade, String sortField, Order sortOrder) throws PersistenceException{
		return findByExample(entidade, -1, -1, sortField, sortOrder, true, false);
	}
	
	/* (non-Javadoc)
	 * @see br.com.framework.negocio.api.BaseNegocio#findByExample(br.com.framework.persistencia.model.BaseEntity)
	 */
	public List<E> findByExample(E entidade) throws PersistenceException{
		return findByExample(entidade, null, null);
	}

}
